package phapp.action.custom;

import static javacommon.util.Struts2JsonHelper.outJson;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseStruts2Action;
import javacommon.jquery.JqueryPageHelper;
import javacommon.jquery.ListRange;
import javacommon.util.StringTool;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;
import phapp.model.custom.Custom;
import phapp.service.qfd.*;
import phapp.service.custom.*;
import phapp.service.luckstar.*;
import phapp.service.data.QfdtextManager;
import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;

public class ClientAction extends BaseStruts2Action
{
   private QfdManager qfdManager;
   private QfdtextManager qfdtextManager;
   private CustomManager customManager;
   private CustqfdManager custqfdManager;
   private StardealManager stardealManager;
   private StaruseManager staruseManager;
   //private FeedbackManager feedbackManager;
   private AttentionManager attentionManager;
   private CustdiscussManager custdiscussManager;
   private CustomFavoriteManager customfavoriteManager;

   public void setQfdManager(QfdManager qfdManager)
   {
      this.qfdManager = qfdManager;
   }

   public void setAttentionManager(AttentionManager attentionManager)
   {
      this.attentionManager = attentionManager;
   }

   public void setCustomManager(CustomManager manager)
   {
      this.customManager = manager;
   }

//   public void setFeedbackManager(FeedbackManager feedbackManager)
//   {
//      this.feedbackManager = feedbackManager;
//   }

   public void setCustqfdManager(CustqfdManager custqfdManager)
   {
      this.custqfdManager = custqfdManager;
   }

   public void setStardealManager(StardealManager stardealManager)
   {
      this.stardealManager = stardealManager;
   }

   public void setStaruseManager(StaruseManager staruseManager)
   {
      this.staruseManager = staruseManager;
   }

   public void setQfdtextManager(QfdtextManager qfdtextManager)
   {
      this.qfdtextManager = qfdtextManager;
   }

   public void setCustdiscussManager(CustdiscussManager custdiscussManager)
   {
      this.custdiscussManager = custdiscussManager;
   }

   public void setCustomFavoriteManager(CustomFavoriteManager manager)
   {
      this.customfavoriteManager = manager;
   }

   public void regist() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("user", request.getParameter("user"));
         jsonParam.put("pass", request.getParameter("pass"));
         jsonParam.put("email", request.getParameter("user"));
         // jsonParam.put("name", request.getParameter("name"));
         // jsonParam.put("sex", request.getParameter("sex"));
         // jsonParam.put("birthday", request.getParameter("birthday"));
         // jsonParam.put("vocation", request.getParameter("vocation"));
         // jsonParam.put("mobile", request.getParameter("mobile"));
         // jsonParam.put("email", request.getParameter("email"));

         result = customManager.customRegist(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "注册失败，参数异常！");
         e.printStackTrace();
      }

      outJson(result);
   }

   public void registInfo() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("user", request.getParameter("user"));
         jsonParam.put("pass", request.getParameter("pass"));
         jsonParam.put("email", request.getParameter("user"));

         jsonParam.put("name", request.getParameter("name"));
         jsonParam.put("sex", request.getParameter("sex"));
         jsonParam.put("city", request.getParameter("city"));
         jsonParam.put("birthday", request.getParameter("birthday"));
         jsonParam.put("filetype", request.getParameter("filetype"));
         jsonParam.put("filedata", request.getParameter("filedata"));

         //result = customManager.customRegist(jsonParam);
         result = customManager.customRegistInfo(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "注册失败，参数异常！");
         e.printStackTrace();
      }

      outJson(result);
   }
   
   public void supplyCustomInfo() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("user", request.getParameter("user"));
         jsonParam.put("name", request.getParameter("name"));
         jsonParam.put("birthday", request.getParameter("birthday"));
         jsonParam.put("city", request.getParameter("city"));
         jsonParam.put("filetype", request.getParameter("filetype"));
         jsonParam.put("filedata", request.getParameter("filedata"));
         // jsonParam.put("pass", request.getParameter("pass"));
         // jsonParam.put("sex", request.getParameter("sex"));
         // jsonParam.put("vocation", request.getParameter("vocation"));
         // jsonParam.put("mobile", request.getParameter("mobile"));
         // jsonParam.put("email", request.getParameter("email"));

         result = customManager.customSupply(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "补充资料失败，参数异常！");
         e.printStackTrace();
      }

      outJson(result);
   }

   public void uploadImage() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("user", request.getParameter("user"));
         jsonParam.put("filetype", request.getParameter("filetype"));
         jsonParam.put("filedata", request.getParameter("filedata"));
         jsonParam.put("name", "");
         jsonParam.put("city", "");
         jsonParam.put("birthday", "");

         result = customManager.customSupply(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "上传头像失败，参数异常！");
         e.printStackTrace();
      }

      outJson(result);
   }

   public void getCustom()
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("user", request.getParameter("user"));

         result = customManager.customDetail(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "获取客户资料失败，参数异常！");
         e.printStackTrace();
      }

      outJson(result);
   }

   public void updateCustom() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("user", request.getParameter("user"));
         jsonParam.put("name", request.getParameter("name"));
         jsonParam.put("city", request.getParameter("city"));
         jsonParam.put("birthday", request.getParameter("birthday"));
         
         if(request.getParameter("pass") == null) jsonParam.put("pass", "");
         else jsonParam.put("pass", request.getParameter("pass"));
         if(request.getParameter("newPass") == null) jsonParam.put("newPass", "");
         else jsonParam.put("newPass", request.getParameter("newPass"));

         if(request.getParameter("sex") == null) jsonParam.put("sex", "0");
         else jsonParam.put("sex", request.getParameter("sex"));
         if(request.getParameter("vocation") == null) jsonParam.put("vocation", "");
         else jsonParam.put("vocation", request.getParameter("vocation"));
         if(request.getParameter("mobile") == null) jsonParam.put("mobile", "");
         else jsonParam.put("mobile", request.getParameter("mobile"));
         if(request.getParameter("email") == null) jsonParam.put("email", "");
         else jsonParam.put("email", request.getParameter("email"));

         result = customManager.customUpdate(jsonParam);

         // String customJson = request.getParameter("customJson");
         // if (null != customJson && !"".equals(customJson))
         // {
         // JSONObject jsonCustom = JSONObject.fromObject(customJson);
         //
         // result = customManager.customUpdate(jsonCustom);
         // }
         // else
         // {
         // result.put("statusCode", 10000);
         // result.put("message", "参数错误！");
         // }
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "修改资料失败，参数异常！");
         e.printStackTrace();
      }

      outJson(result);
   }

   public void login() throws IOException
   {
      // HttpSession userSession = null;
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("user", request.getParameter("user"));
         jsonParam.put("pass", request.getParameter("pass"));
         jsonParam.put("systemType", request.getParameter("systemType"));
         jsonParam.put("systemVersion", request.getParameter("systemVersion"));
         jsonParam.put("version", request.getParameter("version"));
         jsonParam.put("terminalType", request.getParameter("terminalType"));
         jsonParam.put("imei", request.getParameter("imei"));

         result = customManager.customLogin(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "登录异常！");
         e.printStackTrace();
      }

      outJson(result);
   }

   public void resetPasswd() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("user", request.getParameter("user"));
         //jsonParam.put("email", request.getParameter("email"));

         result = customManager.customResetPasswd(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "重置密码失败，参数异常！");
         e.printStackTrace();
      }

      outJson(result);
   }

   public void forecast()
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("user", request.getParameter("user"));

         result = customManager.customForecast(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "测运失败，参数异常！");
         e.printStackTrace();
      }

      outJson(result);
   }

   public void prayLuck()
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("user", request.getParameter("user"));

         result = customManager.customPray(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "开运失败，参数异常！");
         e.printStackTrace();
      }

      outJson(result);
   }

   public void getqfword() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("qfwordtype", request.getParameter("qfwordtype"));

         result = qfdtextManager.getQfwordData(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "获取祈福语失败，参数异常！");
         e.printStackTrace();
      }

      outJson(result);
   }

   public void useLight() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("user", request.getParameter("user"));
         jsonParam.put("qfdid", request.getParameter("qfdid"));
         jsonParam.put("coordinateX", request.getParameter("coordinateX"));
         jsonParam.put("coordinateY", request.getParameter("coordinateY"));
         jsonParam.put("overtflag", request.getParameter("overtflag"));
         jsonParam.put("receiver", request.getParameter("receiver"));
         jsonParam.put("qfdtext", request.getParameter("qfdtext"));

         result = custqfdManager.customUseLight(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "放灯失败，参数异常！");
         e.printStackTrace();
      }

      outJson(result);
   }

   public void getLight()
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("chargeflag", request.getParameter("chargeflag"));
         jsonParam.put("qfdtype", request.getParameter("qfdtype"));
         jsonParam.put("popularflag", request.getParameter("popularflag"));
         jsonParam.put("sellflag", request.getParameter("sellflag"));

         result = qfdManager.getLightData(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "获取祈福灯失败，参数异常！");
         e.printStackTrace();
      }

      outJson(result);
   }

   public void getCustomLight()
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("user", request.getParameter("user"));

         result = custqfdManager.getCustomLightData(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "获取客户祈福灯数据失败，参数异常！");
         e.printStackTrace();
      }

      outJson(result);
   }

   public void getNearLight()
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("user", request.getParameter("user"));
         jsonParam.put("coordinateX", request.getParameter("coordinateX"));
         jsonParam.put("coordinateY", request.getParameter("coordinateY"));

         result = custqfdManager.getNearLightData(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "获取附近祈福灯数据失败，参数异常！");
         e.printStackTrace();
      }

      outJson(result);
   }
   
   @SuppressWarnings("unchecked")
   public void getLatestLight()
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("user", request.getParameter("user"));
         jsonParam.put("pageseq", request.getParameter("pageseq"));
         jsonParam.put("pagenum", request.getParameter("pagenum"));

         result = custqfdManager.getLatestLightData(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "获取最新祈福灯数据失败，参数异常！");
         e.printStackTrace();
      }

      outJson(result);
   }

   public void increase() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("user", request.getParameter("user"));
         jsonParam.put("custqfdid", request.getParameter("custqfdid"));

         result = custqfdManager.customIncreaseOil(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "添加灯油失败，参数异常！");
         e.printStackTrace();
      }

      outJson(result);
   }

   public void buyStar() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("user", request.getParameter("user"));
         jsonParam.put("receiver", request.getParameter("receiver"));
         jsonParam.put("starcount", request.getParameter("starcount"));
         jsonParam.put("paymoney", request.getParameter("paymoney"));

         result = stardealManager.buyStar(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "购买幸运星失败，参数异常！");
         e.printStackTrace();
      }

      outJson(result);
   }

   public void discuss() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("user", request.getParameter("user"));
         jsonParam.put("custqfdid", request.getParameter("custqfdid"));
         jsonParam.put("discusstext", request.getParameter("discusstext"));

         result = custdiscussManager.customDiscuss(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "评论失败，参数异常！");
         e.printStackTrace();
      }

      outJson(result);
   }

   public void getDiscuss() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("custqfdid", request.getParameter("custqfdid"));

         result = custdiscussManager.getDiscussData(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "获取评论失败，参数异常！");
         e.printStackTrace();
      }

      outJson(result);
   }

   public void collectData()
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("user", request.getParameter("user"));
         jsonParam.put("fortune", request.getParameter("fortune"));
         jsonParam.put("luckcolor", request.getParameter("luckcolor"));
         jsonParam.put("luckarticle", request.getParameter("luckarticle"));
         jsonParam.put("luckpos", request.getParameter("luckpos"));
         jsonParam.put("luckvalue", request.getParameter("luckvalue"));
         jsonParam.put("constellation", request.getParameter("constellation"));

         result = customfavoriteManager.collectData(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "收藏失败，参数异常！");
         e.printStackTrace();
      }

      outJson(result);
   }

   public void getCollectData()
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("user", request.getParameter("user"));
         jsonParam.put("pageseq", request.getParameter("pageseq"));
         jsonParam.put("recordnum", request.getParameter("recordnum"));

         result = customfavoriteManager.getCollectData(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "操作异常！");
         e.printStackTrace();
      }

      outJson(result);
   }

   public void getStarObtain() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("user", request.getParameter("user"));

         result = stardealManager.getStarDeal(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "操作失败，参数异常！");
         e.printStackTrace();
      }

      outJson(result);
   }

   public void getStarUse() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("user", request.getParameter("user"));
         jsonParam.put("qfdid", request.getParameter("qfdid"));

         result = staruseManager.getStarUse(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "操作失败，参数异常！");
         e.printStackTrace();
      }

      outJson(result);
   }

   public void feedback() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("user", request.getParameter("user"));
         jsonParam.put("feedback", request.getParameter("feedback"));

         //result = feedbackManager.customFeedback(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "反馈失败，参数异常！");
         e.printStackTrace();
      }

      outJson(result);
   }
   
   public void shareBonus() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("user", request.getParameter("user"));

         result = customManager.cutomShareBonus(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "操作失败，参数异常！");
         e.printStackTrace();
      }

      outJson(result);
   }
   
   
   public void cutomPraise() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("user", request.getParameter("user"));
         jsonParam.put("custid", request.getParameter("custid"));
         jsonParam.put("custqfdid", request.getParameter("custqfdid"));

         //result = custqfdManager.cutomPraiseLight(jsonParam);
         result = customManager.cutomPraise(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "操作失败，参数异常！");
         e.printStackTrace();
      }

      outJson(result);
   }
   
   public void attention() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("user", request.getParameter("user"));
         jsonParam.put("attentionuserid", request.getParameter("attentionuserid"));
         jsonParam.put("attentionstatus", request.getParameter("attentionstatus"));

         result = attentionManager.customAttention(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "操作失败，参数异常！");
         e.printStackTrace();
      }

      outJson(result);
   }

   public void queryAttention() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("user", request.getParameter("user"));
         jsonParam.put("attentionuserid", request.getParameter("attentionuserid"));
         
         result = attentionManager.queryAttentionData(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "操作失败，参数异常！");
         e.printStackTrace();
      }

      outJson(result);
   }
   
   public void getAttention() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();

      try
      {
         HttpServletRequest request = getRequest();
         JSONObject jsonParam = new JSONObject();
         jsonParam.put("user", request.getParameter("user"));

         result = attentionManager.getAttentionData(jsonParam);
      }
      catch(Exception e)
      {
         result.put("statusCode", 10000);
         result.put("message", "操作失败，参数异常！");
         e.printStackTrace();
      }

      outJson(result);
   }
   
   
   
}
